package com.ruoyi.project.onlineExperimentOrder.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.onlineExperimentOrder.domain.OnlineExperimentOrder;
import com.ruoyi.project.onlineExperimentOrder.mapper.OnlineExperimentOrderMapper;
import com.ruoyi.project.onlineExperimentOrder.service.OnlineExperimentOrderService;
import com.ruoyi.project.online_experiment.domain.OnlineExperiment;
import com.ruoyi.project.online_experiment.mapper.OnlineExperimentMapper;
import com.ruoyi.project.online_experiment.service.IOnlineExperimentService;
import com.ruoyi.project.online_experimentStu.mapper.OnlineExperimentStuMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName OnlineExperimentOrder    学生预约开放实验
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-07-27 13:45
 **/
@Controller
@RequestMapping("/system/onlineExperimentOrder")
public class OnlineExperimentOrderController extends BaseController {

    private String prefix = "system/onlineExperimentOrder";

    @Autowired
    private IOnlineExperimentService onlineExperimentService;

    @Autowired
    private OnlineExperimentOrderService onlineExperimentOrderService;

    @Autowired
    private OnlineExperimentOrderMapper onlineExperimentOrderMapper;

    @Autowired
    private OnlineExperimentStuMapper onlineExperimentStuMapper;

    @RequiresPermissions("system:onlineExperimentOrder:view")
    @GetMapping()
    public String experiment()
    {
        return prefix + "/onlineExperimentOrder";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:onlineExperimentOrder:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OnlineExperiment onlineExperiment)
    {
        startPage();
        List<OnlineExperiment> list = onlineExperimentService.selectOnlineExperimentList(onlineExperiment);
        /**
         * 查找学生预约信息，改变预约状态
         */
        return getDataTable(list);
    }


    /**
     * 新增【请填写功能名称】
     */
    @GetMapping("/add")
    public String add(int id, ModelMap modelMap)
    {
        modelMap.addAttribute("onlineExperimentId",id);
        return prefix + "/add";
    }

    /**
     * 新增保存【请填写功能名称】
     */
    @Log(title = "【用户预约实验信息】", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OnlineExperimentOrder onlineExperimentOrder)
    {
        long userId=getSysUser().getUserId();
        onlineExperimentOrder.setUserId(userId);
        /**
         * 判断实验是否已经预约
         */
        OnlineExperimentOrder onlineExperimentOrder1=onlineExperimentOrderService.getOnlineExperimentOrder(onlineExperimentOrder);
        if(StringUtils.isNull(onlineExperimentOrder1) || onlineExperimentOrder1==null){
            /**
             * 预约实验
             */
            return toAjax(onlineExperimentOrderService.onlineExperimentOrder(onlineExperimentOrder));
        }else{
            /**
             * 更新实验，删除之前同意时布置的实验
             */
/**
 * 根据用户id和虚拟项目id查询预约信息
 */
            /**
             * 判断是否有对应的项目信息
             */
            if(!StringUtils.isNull(onlineExperimentOrder1.getExperimentTeaId()) || onlineExperimentOrder1.getExperimentTeaId()!=null){
                /**
                 * 删除发布实验信息
                 */
                onlineExperimentStuMapper.deleteOnlineExperimentTea(onlineExperimentOrder1.getExperimentTeaId());
                onlineExperimentStuMapper.deleteOnlineExperimentStu(onlineExperimentOrder1.getExperimentTeaId());
                onlineExperimentOrder.setExperimentTeaId(null);
            }
            return toAjax(onlineExperimentOrderService.updateOrder(onlineExperimentOrder));
        }
    }


    /**
     * 获取自己名下的所有实验预约信息
     * @return
     */
    @RequiresPermissions("system:onlineExperimentOrders:view")
    @GetMapping("/getAllOrder")
    public String getAllExperimentOrder()
    {
        return prefix + "/onlineExperimentOrders";
    }


    /**
     * 查询【预约信息】列表
     */
    @RequiresPermissions("system:onlineExperimentOrders:list")
    @PostMapping("/orderList")
    @ResponseBody
    public TableDataInfo list(OnlineExperimentOrder onlineExperimentOrder)
    {
        long userId = getSysUser().getUserId();
        onlineExperimentOrder.setUserId(userId);
        startPage();
        List<OnlineExperimentOrder> list = onlineExperimentOrderService.getOrderList(onlineExperimentOrder);
        return getDataTable(list);
    }


    /**
     * 学生撤销实验预约
     * @param data
     * @return
     */
    @RequiresPermissions("system:onlineExperimentOrders:remove")
    @PostMapping("/revocation")
    @ResponseBody
    public AjaxResult revocation(String data){
        /**
         * 获取参数
         */
        String[] array=data.split(",");
        OnlineExperimentOrder onlineExperimentOrder=new OnlineExperimentOrder();
        /**
         * 赋值
         */
        onlineExperimentOrder.setOnlineExperimentId(Integer.valueOf(array[0]));
        onlineExperimentOrder.setUserId(Long.valueOf(array[1]));
        /**
         * 查询是否有此条数据
         */
        OnlineExperimentOrder onlineExperimentOrder1=onlineExperimentOrderService.getOnlineExperimentOrder(onlineExperimentOrder);
        /**
         * 判断是否已经同意预约，添加了实验
         */
        if(StringUtils.isEmpty(onlineExperimentOrder1.getExperimentTeaId()) || onlineExperimentOrder1.getExperimentTeaId()==null){
            return toAjax(onlineExperimentOrderService.revocation(onlineExperimentOrder));
        }else{
            return AjaxResult.error("撤销失败");
        }
    }
}
